home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / heartbeat.lha / HeartBeat / cload_file.s < prev    next >
Text File  |  1992-04-20  |  3KB  |  106 lines

  1. *******************************************************************************
  2. * Load_File:    independent module to load an entire file in memory.
  3. * ---------
  4. *    This routine is especially handy for ASCII text files since
  5. *    it marks the end of your text with a LONG of 0s.
  6. *    The returned size INCLUDES this extra LONG so that your FreeMem()
  7. *    is straight forward.
  8. *
  9. *******************************************************************************
  10.  
  11.         include    std
  12.  
  13.         XREF    _DOSBase        ;use Lattice's ptr to dos.library
  14.  
  15. ; EXPORTS
  16.         XDEF    _load_file
  17.  
  18. *******************************************************************************
  19. FENCE_SPACE    equ    8            ;# of $00s to add beyond EOF
  20. *******************************************************************************
  21.  
  22. ;-----------------------------------------------
  23. ; BOOL = load_file( char *filename , struct FileCache *fc );
  24. ;   D0 =              4(SP)            8(SP)
  25. ;-----------------------------------------------
  26.  
  27. SAVED_REGS    equ    (6*4)+(5*4)        ;room for MOVEM regs
  28.  
  29. _load_file    move.l    4(SP),a0        ;get fname argument from stack
  30.         movem.l    d2-d7/a2-a6,-(SP)    ;push all Amiga regs
  31.         move.l    a0,a4            ;save filename
  32.  
  33.         bsr    get_file_size        ;D6/D7 size,filehandle
  34.         beq    ldfile_error        ;if unable to find file: quit now
  35.         
  36.         add.l    #FENCE_SPACE,D6        ;extra room for fence space
  37.  
  38.         move.l    4.w,a6
  39.         move.l    d6,d0            ;wanted file buffer size
  40.         moveq    #MEMF_PUBLIC,d1        ;any RAM will do.
  41.         EXEC    AllocMem
  42.         tst.l    d0            ;did I get the file buffer?
  43.         beq    ldfile_error        ;no : FAIL !
  44.  
  45.         move.l    d0,a5            ;A5 -> buffer memory
  46.  
  47.         move.l    _DOSBase,a6
  48.         move.l    d7,d1            ;file handle
  49.         move.l    a5,d2            ;-> buffer
  50.         move.l    d6,d3            ;file size +FENCE_SPACE
  51.         sub.l    #FENCE_SPACE,d3        ;adjust to pure file size
  52.         DOS    Read            ;read entire file into buffer
  53.  
  54.         lea    -FENCE_SPACE(a5,d6.L),a0  ;->> last FENCE bytes of buffer
  55.         clr.b    (a0)+            ;mark last bytes
  56.         clr.b    (a0)+
  57.         clr.b    (a0)+
  58.         clr.b    (a0)+            ;to delimit text
  59.         clr.b    (a0)+
  60.         clr.b    (a0)+
  61.         clr.b    (a0)+
  62.         clr.b    (a0)+
  63.  
  64.         move.l    d7,d1
  65.         DOS    Close
  66.  
  67.         move.l    SAVED_REGS+8(SP),a0    ;get ptr to FileCache struct
  68.         move.l    a5,(a0)
  69.         move.l    d6,4(a0)        ;fill in struct
  70.  
  71.         movem.l    (SP)+,d2-d7/a2-a6
  72.         moveq    #-1,d0
  73.         rts
  74.  
  75. ldfile_error    movem.l    (SP)+,d2-d7/a2-a6
  76.         moveq    #0,d0            ;return FALSE
  77. dummy        rts
  78. ;-----------------------------------------------
  79. ; A4 -> C- filename
  80. ; OPEN file, SEEK to EOF, RECORD size, SEEK back to beginning
  81. ; OUTPUT : D7 = filehandle
  82. ;       D6 = size
  83. ;-----------------------------------------------
  84.  
  85. get_file_size    move.l    _DOSBase,a6
  86.         move.l    a4,d1            ;D1 -> filename
  87.         move.l    #MODE_OLDFILE,d2
  88.         DOS    Open            ;open file
  89.         move.l    d0,d7            ;D7 = file handle
  90.         req                ;if Open failed: return EQ
  91.  
  92.         move.l    d7,d1
  93.         moveq    #0,d2
  94.         move.l    #OFFSET_END,d3        ;goto EOF
  95.         DOS    Seek            ;returns old file pointer (0)
  96.  
  97.         move.l    d7,d1
  98.         moveq    #0,d2
  99.         move.l    #OFFSET_BEGINNING,d3    ;go back to start
  100.         DOS    Seek            ;old pos (EOF) = size
  101.  
  102.         move.l    d0,d6            ;record file size (return NE)
  103.         rts        
  104. ;-----------------------------------------------
  105. *******************************************************************************
  106.